<template>
	<!-- 父级调用 FlowNodeTmp -->
	<div>
		<a-modal v-model:visible="visibleFlowModel" :width="600" :title="`${formState2.key ? '修改申请' : '审批'}`" @ok="updateStatus">
			<a-form ref="formRef2" :model="formState2" v-bind="{labelCol: { style: { width: '100px' } }}">
				<a-form-item label="审批状态" name="status" :rules="{required:true, message:'选择审批状态'}">
					<a-radio-group v-model:value="formState2.status" button-style="solid">
						<span v-for="item in flowStatus" :key="item.status">
							<a-radio v-if="!(formState2.pid === 0 && item.status ===-1 ) && item.status !== 0" :value="item.status">{{ item.name }}</a-radio>
						</span>
						<!-- <a-radio value="-1">拒绝</a-radio> -->
						<!-- <a-radio-button value="d">提交给其他角色</a-radio-button> -->
					</a-radio-group>
				</a-form-item>
				<template v-if="formState2.status !== 2">
					<a-form-item label="下步审批(可选)" name="role_ids" :rules="{required:formState2.status === 2, message:'请选择角色'}">
						<a-radio-group v-model:value="role_ids" style="width: 100%">
							<a-row>
								<a-col v-for="(item,i) in roles" :key="item.id" :span="8">
									<!-- :disabled="item.id == formState2.role_id" -->
									<a-radio :value="item.id" :checked="false">{{ i+1 }}:{{ item.name }}</a-radio>
								</a-col>
								<a-col :span="8">
									<a-radio :value="0" :checked="false">无</a-radio>
								</a-col>
							</a-row>
						</a-radio-group>
					</a-form-item>
				</template>
				<a-form-item label="备注" name="remark">
					<a-input v-model:value="formState2.remark" />
				</a-form-item>
			</a-form>
		</a-modal>
	</div>
</template>
<script setup>
/**
 * 项目节点状态操作 表单
 */
import { ref, watch, onMounted, getCurrentInstance } from "vue";
import { post, treeToList } from "../utils";
import { useRequest } from "vue-request";
import flowNodeStatusButton from "@/components/FlowNodeStatusButton";
import router from "../router";
import dayjs from "dayjs";
const { proxy } = getCurrentInstance();
const roles = ref();
const roleIdx = ref(0);
const flowNodeIdx = ref(0);
const flowStatus = ref();
const flowSource = ref();
const flowNodes = ref();
const role_ids = ref(0);
flowStatus.value = [
	{ status: -1, name: "退回修改" },
	{ status: 0, name: "待审批" },
	{ status: 1, name: "审批通过" },
	{ status: 2, name: "审批完成" }
];
const props = defineProps({
	flowId: {
		type: Number,
		required: false,
		default: 0
	}
	// code: {
	// 	type: String,
	// 	required: true,
	// 	default: ""
	// },
	// defaultExpandAll: {
	// 	type: Boolean,
	// 	required: true,
	// 	default: true
	// }
});

const visibleFlowModel = ref(false);
const formRef2 = ref();
const formState2 = ref({});
// 打开角色审批表单 权限拦截处
const showFlowModel = (record, depts, dept_ids) => {
	formState2.value = { ...record };
	formState2.value.status = 1;
	if (!proxy.$auth.isDept(formState2.value.role_id)) {
		return;
	}
	roles.value = depts;
	if (depts.lenght > 0) {
		var idx = depts.findIndex(e => {
			return e.id === formState2.value.role_id;
		});
		//console.log(idx, depts[idx - 1].id);
		idx = idx + 1;

		if (idx !== 1 && depts.lenght !== idx) {
			formState2.value.role_ids = depts[idx - 1].id;
		} else {
			formState2.value.role_ids = 0;
		}
		role_ids.value = formState2.value.role_ids;
		// if (typeof dept_ids === "string") {
		// 	role_ids.value = dept_ids.split(",").map((d) => parseInt(d));
		// } else if (typeof dept_ids === "object") {
		// 	role_ids.value = dept_ids;
		// }
	}

	visibleFlowModel.value = true;
};
defineExpose({
	showFlowModel
});
//定义emit
const emit = defineEmits(["change"]);
const updateStatus = () => {
	formRef2.value
		.validate()
		.then(() => {
			proxy.$message.destroy();

			const params = { ...formState2.value };
			if (role_ids.value) {
				params.role_ids = role_ids.value;
				// params.role_ids = role_ids.value.join(",");
			}
			post("flow/updateNodeStatus", params)
				.then(() => {
					proxy.$message.success("更新成功", 1);
					visibleFlowModel.value = false;
					emit("change", { act: "updateStatus", status: 1 });
				})
				.catch(err => {
					proxy.$message.error(err);
				});
		})
		.catch(err => {
			//console.log(err);
		});
};
watch(visibleFlowModel, value => {
	if (value) formRef2.value?.clearValidate();
});
// run();
</script>